/*
 * @lc app=leetcode.cn id=405 lang=cpp
 *
 * [405] 数字转换为十六进制数
 */

// @lc code=start
class Solution
{
public:
  string toHex(int num)
  {
    if (num == 0) return "0";
    unordered_map<int, char> m = {
        make_pair(0, '0'),
        make_pair(1, '1'),
        make_pair(2, '2'),
        make_pair(3, '3'),
        make_pair(4, '4'),
        make_pair(5, '5'),
        make_pair(6, '6'),
        make_pair(7, '7'),
        make_pair(8, '8'),
        make_pair(9, '9'),
        make_pair(10, 'a'),
        make_pair(11, 'b'),
        make_pair(12, 'c'),
        make_pair(13, 'd'),
        make_pair(14, 'e'),
        make_pair(15, 'f')};
    unsigned int ui = (unsigned int) num;
    int mask        = 0x0000000f;
    string hex      = "";
    while (ui != 0) {
      hex = m[mask & ui] + hex;
      ui >>= 4;
    }
    return hex;
  }
};
// @lc code=end
